/*
 * Copyright 2008-2009 Daniel Freitas
 *
 * This file is part of Army Creator.
 * 
 * Army Creator is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Army Creator is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Army Creator.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.armycreator.datastore.test

import com.googlecode.armycreator.datastore.test.PersistentDummy
import com.googlecode.armycreator.datastore.test.PersistentDummyDao
import com.googlecode.armycreator.datastore.test.TransactionService

/**
 * Simulates a service that delimites a transaction boundary.
 *
 * @author <a href="mailto:daniel.mfreitas@gmail.com">Daniel Freitas</a>
 */
public class TransactionTestService implements TransactionService {
    PersistentDummyDao persistentDummyDao;

    Long id1 = null;
    Long id2 = null;

    @Override
    public void updateTwoObjectsAndThrowsRumtimeException(PersistentDummy object1, PersistentDummy object2) {
        persistentDummyDao.makePersistent(object1);
        persistentDummyDao.makePersistent(object2);

        throw new IllegalArgumentException("You should rollback");
    }

    @Override
    public void updateTwoObjectsAndThrowsCheckedException(PersistentDummy object1, PersistentDummy object2) throws Exception {
        persistentDummyDao.makePersistent(object1);
        persistentDummyDao.makePersistent(object2);

        throw new Exception("Although this exception is thrown, the changes "
                + "should have been committed since this was a checked exception.");
    }

    @Override
    public void updateTwoObjectsAndThrowsCheckedExceptionAnnotatedForRollback(PersistentDummy object1, PersistentDummy object2)
    throws Exception {
        persistentDummyDao.makePersistent(object1);
        persistentDummyDao.makePersistent(object2);

        throw new Exception("Ok. This is a checked exception. The default behaviour is to not rollback on checked "
                + "exception, but since we annotated it to do so the transaction should have been rolled back.");
    }
}
